//
//  YPVerifyViewController.swift
//  YourPassword
//
//  Created by 李冲 on 15/11/26.
//  Copyright © 2015年 Saye. All rights reserved.
//

import UIKit
import LocalAuthentication
import SnapKit

class YPVerifyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        YPGlobal.applyTheme(self)
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.flatWhiteColor()
        self.initSubviews()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.showVerify()
    }
    
    func initSubviews() {
        let verifyButton = UIButton()
        verifyButton.setTitle("verify", forState: .Normal)
        verifyButton.addTarget(self, action: Selector("showVerify"), forControlEvents: .TouchUpInside)
        self.view.addSubview(verifyButton)
        verifyButton.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.view)
            make.width.equalTo(100)
            make.height.equalTo(44)
        }
        
        let label = UILabel()
        label.text = "验证您的身份"
        self.view.addSubview(label)
        label.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(verifyButton.snp_top).offset(-10)
            make.centerX.equalTo(self.view)
        }
    }
    
    func showVerify() {
        let context = LAContext()
        if (context.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil)) {
            context.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "login", reply: { (succeed, _) -> Void in
                if (succeed) {
                    dispatch_async(dispatch_get_main_queue(), {() -> Void in
                        self.dismissViewControllerAnimated(true, completion: nil)
                    })
                }

            })
        }
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
